﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace Crossfader
{
    public partial class MediaElement : UserControl
    {
        private ObservableCollection<bool> IsChannelBusy = new ObservableCollection<bool>();
        private int ChannelCollectionCount = 5;

        #if !Silverlight  
            private ObservableCollection<MediaPlayer> ChannelCollection = new ObservableCollection<MediaPlayer>();
        #else
            private ObservableCollection<System.Windows.Controls.MediaElement> ChannelCollection = new ObservableCollection<System.Windows.Controls.MediaElement>();
        #endif

        public MediaElement()
        {
            InitializeComponent();

            #if !Silverlight  
                ChannelCollection.Add(new MediaPlayer());
                ChannelCollection[0].MediaEnded += new EventHandler(MediaElement_MediaEnded);

                ChannelCollection.Add(new MediaPlayer());
                ChannelCollection[1].MediaEnded += new EventHandler(MediaElement1_MediaEnded);

                ChannelCollection.Add(new MediaPlayer());
                ChannelCollection[2].MediaEnded += new EventHandler(MediaElement2_MediaEnded);

                ChannelCollection.Add(new MediaPlayer());
                ChannelCollection[3].MediaEnded += new EventHandler(MediaElement3_MediaEnded);

                ChannelCollection.Add(new MediaPlayer());
                ChannelCollection[4].MediaEnded += new EventHandler(MediaElement4_MediaEnded);
            #else
                ChannelCollection.Add(Clannel1Base);
                ChannelCollection.Add(Clannel2Base);
                ChannelCollection.Add(Clannel3Base);
                ChannelCollection.Add(Clannel4Base);
                ChannelCollection.Add(Clannel5Base);
            #endif

            for (int x = 0; x < ChannelCollectionCount; x++)
            { 
                IsChannelBusy.Add(false);
            } 
        }

        public static readonly DependencyProperty AutoPlayProperty = DependencyProperty.Register("AutoPlay", typeof(bool), typeof(MediaElement), new PropertyMetadata(new PropertyChangedCallback(OnAutoPlayChanged)));

        public bool AutoPlay
        {
            get { return (bool)GetValue(AutoPlayProperty); }
            set { SetValue(AutoPlayProperty, value); }
        }

        private static void OnAutoPlayChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
 
        }
 
        public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(string), typeof(MediaElement), new PropertyMetadata(new PropertyChangedCallback(OnSourceChanged)));

        public string Source
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        private static void OnSourceChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            MediaElement ThisElement = DpObj as MediaElement;

            if (ThisElement != null)
            {
                ThisElement.SetSource(ThisElement.Source);
            }
        }

        public void SetSource(string NewUri)
        {  
            int x = 0;

            while( x > -1 )
            {
                if (x > (ChannelCollectionCount - 1)) 
                {
                    x = 0;
                }
                if (!IsChannelBusy[x])
                {
                    IsChannelBusy[x] = true;
                    SetSource(NewUri, ChannelCollection[x]);
                    x = -1;
                    break;
                }
                x++;
            }
        }

        #if !Silverlight
            private void SetSource(string NewUri, MediaPlayer ClannelBase)
            { 
                ClannelBase.Open(new Uri(NewUri, UriKind.Relative));
                ClannelBase.Play(); 
            }
        #else
            private void SetSource(string NewUri, System.Windows.Controls.MediaElement ClannelBase)
            { 
                Uri MediaItem = new Uri(NewUri, UriKind.RelativeOrAbsolute);
                ClannelBase.Source = MediaItem;
                ClannelBase.AutoPlay = true;
                ClannelBase.Play();
            }
        #endif

        private void ClannelBase_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            //IsChannelBusy[((int)(((System.Windows.Controls.MediaElement)(sender)).Tag))] = false;
            System.Windows.Controls.MediaElement MyPointer = ((System.Windows.Controls.MediaElement)(sender));
            string MyIndexer = ((string)(MyPointer.Tag));
            int TheFrakingRealIntThatCanNotBeFrakingUnboxed = int.Parse(MyIndexer);

            IsChannelBusy[TheFrakingRealIntThatCanNotBeFrakingUnboxed] = false;
        }
 
        private void ClannelBase_MediaEnded(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.MediaElement MyPointer = ((System.Windows.Controls.MediaElement)(sender));
            string MyIndexer = ((string)(MyPointer.Tag));
            int TheFrakingRealIntThatCanNotBeFrakingUnboxed = int.Parse(MyIndexer);

            IsChannelBusy[TheFrakingRealIntThatCanNotBeFrakingUnboxed] = false;
        }
  
        void MediaElement_MediaEnded(object sender, EventArgs e)
        {
            IsChannelBusy[0] = false;
        }

        void MediaElement1_MediaEnded(object sender, EventArgs e)
        {
            IsChannelBusy[1] = false;
        }

        void MediaElement2_MediaEnded(object sender, EventArgs e)
        {
            IsChannelBusy[2] = false;
        }

        void MediaElement3_MediaEnded(object sender, EventArgs e)
        {
            IsChannelBusy[3] = false;
        }

        void MediaElement4_MediaEnded(object sender, EventArgs e)
        {
            IsChannelBusy[4] = false;
        }
         
    }
}
